import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:tot_android_flutter/base/BasePage.dart';
import 'package:tot_android_flutter/common/widget/app_bar.dart';
import 'package:tot_android_flutter/pages/tabpage/mine/mine_controller.dart';
import 'package:tot_android_flutter/router/AppRoutes.dart';
import 'package:tot_android_flutter/themes/AppTextStyles.dart';
import 'package:tot_android_flutter/themes/color_palettes.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';

class MinePage extends BasePage<MineController> {
  const MinePage({Key? key}) : super(key: key);

  @override
  AppBar? buildAppBar() {
    return commonAppBar();
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(children: [
      Positioned.fill(
        child: SingleChildScrollView(
          controller: controller.scrollController,
          child: Column(
            children: [
              SizedBox(height: 12.w),
              _buildUserInfo(),
              SizedBox(height: 12.w),
              _buildMenu(),
              SizedBox(height: 12.w),
              _buildOrder(),
              SizedBox(height: 12.w),
              _buildOther(context),
              Container(
                height: 500,
                color: Colors.black,
              ),
              // ... 其他子部件 ...
            ],
          ),
        ),
      ),
      Positioned(top: 0, left: 0, right: 0, child: _buildTop())
    ]);
  }

  Widget _buildMenu() {
    return Padding(
        padding: EdgeInsets.symmetric(horizontal: 8.w),
        child: Card(
          color: Colors.white,
          elevation: 4.0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(15.w),
          ),
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: 14.w),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: controller.menuList.map((item) {
                return Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(item.iconData, size: 28.w),
                      Text(
                        item.title,
                        style: AppTextStyles.bodyfirst,
                      )
                    ],
                  ),
                );
              }).toList(),
            ),
          ),
        ));
  }

  Widget _buildOrder() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Card(
        color: Colors.white,
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.w)),
        elevation: 4.w,
        child: Padding(
            padding: EdgeInsets.symmetric(vertical: 14.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Padding(
                    padding: EdgeInsets.fromLTRB(12.w, 0, 0, 0),
                    child: const Text("订单")),
                SizedBox(height: 8.w),
                Row(
                  children: controller.orderStatus.map((item) {
                    return Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(item.iconData, size: 28.w),
                          Text(
                            item.title,
                            style: AppTextStyles.bodyfirst,
                          )
                        ],
                      ),
                    );
                  }).toList(),
                ),
              ],
            )),
      ),
    );
  }


  Widget _buildOther(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      child: Card(
        color: Colors.white,
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.w)),
        elevation: 4.w,
        child: Padding(
            padding: EdgeInsets.symmetric(vertical: 0.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GridView.count(
                  crossAxisCount: 4, // 设置每行显示的数量
                  shrinkWrap: true, // 让GridView根据子项的大小自动收缩
                  physics: NeverScrollableScrollPhysics(), // 禁止滚动
                  children: controller.otherMenuList.map((item) {
                    return Container(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(item.iconData, size: 28.w),
                          Text(
                            item.title,
                            style: AppTextStyles.bodyfirst,
                          )
                        ],
                      ),
                    );
                  }).toList(),
                ),
              ],
            )),
      ),
    );
  }

  Widget _buildUserInfo() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(width: 16.w),
            CircleAvatar(
              backgroundImage:
                  AssetImage("assets/image/ic_default_avatar.webp"),
              radius: 30.w,
            ),
            SizedBox(width: 8.w),
            _buildUserName()
          ],
        ),
        const Spacer(), // 使用 Spacer 来代替 Expanded
        _buildKeFu(),
        SizedBox(width: 16.w),
        _buildJifen(),
        SizedBox(width: 16.w),
      ],
    );
  }

  Widget _buildTop() {
    return controller.flag.value
        ? Container(
            color: ColorPalettes.instance.background,
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 12.w),
              child: Row(
                children: [
                  SizedBox(width: 16.w),
                  _buildUserName(),
                  const Spacer(), // 使用 Spacer 来代替 Expanded
                  _buildKeFu(),
                  SizedBox(width: 16.w),
                  _buildJifen(),
                  SizedBox(width: 16.w),
                ],
              ),
            ))
        : Container();
  }

  Widget _buildUserName() {
    return GestureDetector(
      onTap: () {
        Get.toNamed(AppPages.LOGIN);
      },
      child: controller.userEntity.value != null
          ? Text("${controller.userEntity.value!.nickname}")
          : Text("登录/注册"),
    );
  }

  Widget _buildKeFu() {
    return GestureDetector(
      onTap: () {
        Get.toNamed(AppPages.LOGIN);
      },
      child: Text("客服"),
    );
  }

  Widget _buildJifen() {
    return GestureDetector(
      onTap: () {
        Get.toNamed(AppPages.LOGIN);
      },
      child: Row(
        children: [
          Text("积分"),
          SizedBox(width: 4.w),
          Text(
              "${controller.userEntity.value != null ? controller.userEntity.value!.coinCount : "---"}"),
          SizedBox(width: 16.w),
        ],
      ),
    );
  }
}
